In the field of computer systems, considerable effort has been expended on the task of allocating memory to data objects. For the purposes of this discussion, the term object refers to a data structure represented in a computer system's memory. Other terms sometimes used for the same concept are record and structure. An object may be identified by a reference, a relatively small amount of information that can be used to access the object. A reference can be represented as a “pointer” or a “machine address,” which may require, for instance, only sixteen, thirty-two, or sixty-four bits of information, although there are other ways to represent a reference.
In some systems, which are usually known as “object oriented,” objects may have associated methods, which are routines that can be invoked by reference to the object. They also may belong to a class, which is an organizational entity that may contain method code or other information shared by all objects belonging to that class. In the discussion that follows, though, the term object will not be limited to such structures; it will additionally include structures with which methods and classes are not associated.
The invention to be described below is applicable to systems that allocate memory to objects dynamically. Not all systems employ dynamic allocation. In some computer languages, source programs can be so written that all objects to which the program's variables refer are bound to storage locations at compile time. This storage-allocation approach, sometimes referred to as “static allocation,” is the policy traditionally used by the Fortran programming language, for example.
Even for compilers that are thought of as allocating objects only statically, of course, there is often a certain level of abstraction to this binding of objects to storage locations. Consider the typical computer system 100 depicted in FIG. 1, for example. Data, and instructions for operating on them, that a microprocessor 110 uses may reside in on-board cache memory or be received from further cache memory 120, possibly through the mediation of a cache controller 130. That controller 130 can in turn receive such data from system read/write memory (“RAM”) 140 through a RAM controller 150 or from various peripheral devices through a system bus 160. Additionally, instructions and data may be received from other computer systems via a communication interface 180. The memory space made available to an application program may be “virtual” in the sense that it may actually be considerably larger than RAM 140 provides. So the RAM contents will be swapped to and from a system disk 170.
Additionally, the actual physical operations performed to access some of the most-recently visited parts of the process's address space often will actually be performed in the cache 120 or in a cache on board microprocessor 110 rather than on the RAM 140, with which those caches swap data and instructions just as RAM 140 and system disk 170 do with each other.
A further level of abstraction results from the fact that an application will often be run as one of many processes operating concurrently with the support of an underlying operating system. As part of that system's memory management, the application's memory space may be moved among different actual physical locations many times in order to allow different processes to employ shared physical memory devices. That is, the location specified in the application's machine code may actually result in different physical locations at different times because the operating system adds different offsets to the machine-language-specified location.
The use of static memory allocation in writing certain long-lived applications makes it difficult to restrict storage requirements to the available memory space. Abiding by space limitations is easier when the platform provides for dynamic memory allocation, i.e., when memory space to be allocated to a given object is determined only at run time.
Dynamic allocation has a number of advantages, among which is that the run-time system is able to adapt allocation to run-time conditions. For example, the programmer can specify that space should be allocated for a given object only in response to a particular run-time condition. The C-language library function malloc( ) is often used for this purpose. Conversely, the programmer can specify conditions under which memory previously allocated to a given object can be reclaimed for reuse. The C-language library function free( ) results in such memory reclamation. Because dynamic allocation provides for memory reuse, it facilitates generation of large or long-lived applications, which over the course of their lifetimes may employ objects whose total memory requirements would greatly exceed the available memory resources if they were bound to memory locations statically.
Particularly for long-lived applications, though, allocation and reclamation of dynamic memory must be performed carefully. If the application fails to reclaim unused memory-or, worse, loses track of the address of a dynamically allocated segment of memory-its memory requirements will grow over time to exceed the system's available memory. This kind of error is known as a “memory leak.” Another kind of error occurs when an application reclaims memory for reuse even though it still maintains a reference to that memory. If the reclaimed memory is reallocated for a different purpose, the application may inadvertently manipulate the same memory in multiple inconsistent ways. This kind of error is known as a “dangling reference.”
A way of reducing the likelihood of such leaks and related errors is to provide memory-space reclamation in a more automatic manner. Techniques used by systems that reclaim memory space automatically are commonly referred to as garbage collection. Garbage collectors operate by reclaiming space that they no longer consider “reachable.” Statically allocated objects represented by a program's global variables are normally considered reachable throughout a program's life. Such objects are not ordinarily stored in the garbage collector's managed memory space, but they may contain references to dynamically allocated objects that are, and such objects are considered reachable. Clearly, an object referred to in the processor's call stack is reachable, as is an object referred to by register contents. And an object referred to by any reachable object is also reachable. As used herein, a call stack is a data structure that corresponds to a process or thread and stores state information, such as local variables, register contents and program counter values, associated with nested routines within the process or thread. A call stack is usually thought of as divided into stack frames associated with respective calls of the nested routines.
The use of garbage collectors is advantageous because, whereas a programmer working on a particular sequence of code can perform his task creditably in most respects with only local knowledge of the application at any given time, memory allocation and reclamation require a global knowledge of the program. Specifically, a programmer dealing with a given sequence of code does tend to know whether some portion of memory is still in use for that sequence of code, but it is considerably more difficult for him to know what the rest of the application is doing with that memory. By tracing references from some conservative notion of a root set, e.g., global variables, registers, and the call stack, automatic garbage collectors obtain global knowledge in a methodical way. By using a garbage collector, the programmer is relieved of the need to worry about the application's global state and can concentrate on local-state issues, which are more manageable. The result is applications that are more robust, having no dangling references and fewer memory leaks.
Garbage collection mechanisms can be implemented by various parts and levels of a computing system. One approach is simply to provide them as part of a batch compiler's output. Consider FIG. 2's simple batch-compiler operation, for example. A computer system executes in accordance with compiler object code and therefore acts as a compiler 200. The compiler object code is typically stored on a medium such as FIG. 1's system disk 170 or some other machine-readable medium, and it is loaded into RAM 140 to configure the computer system to act as a compiler. In some cases, though, the compiler object code's persistent storage may instead be provided in a server system remote from the machine that performs the compiling. The electrical signals that carry the digital data by which the computer systems exchange that code are examples of the kinds of electromagnetic signals by which the computer instructions can be communicated. Others include radio waves, microwaves, and both visible and invisible light.
The input to the compiler is the application source code, and the end product of the compiler process is application object code. This object code defines an application 210, which typically operates on input such as mouse clicks, etc., to generate a display or some other type of output. This object code implements the relationship that the programmer intends to specify by his application source code. In one approach to garbage collection, the compiler 200, without the programmer's explicit direction, additionally generates code that automatically reclaims unreachable memory space.
Even in this simple case, though, there is a sense in which the application does not itself provide the entire garbage collector. Specifically, the application will typically call upon the underlying operating system's memory-allocation functions. And the operating system may in turn take advantage of various hardware that lends itself particularly to use in garbage collection. So even a very simple system may disperse the garbage collection mechanism over a number of computer system layers.
To get some sense of the variety of system components that can be used to implement garbage collection, consider FIG. 3's example of a more complex way in which various levels of source code can result in the machine instructions that a processor executes. In the FIG. 3 arrangement, the human applications programmer produces source code 310 written in a high-level language. A compiler 320 typically converts that code into “class files.” These files include routines written in instructions, called “byte codes” 330, for a “virtual machine” that various processors can be configured to emulate. This conversion into byte codes is almost always separated in time from those codes' execution, so FIG. 3 divides the sequence into a “compile-time environment” 300 separate from a “run-time environment” 340, in which execution occurs. One example of a high-level language for which compilers are available to produce such virtual-machine instructions is the Java™ programming language. (Java is a trademark or registered trademark of Sun Microsystems, Inc., in the United States and other countries.)
Most typically, the class files' byte-code routines are executed by a processor under control of a virtual-machine process 350. That process emulates a virtual machine from whose instruction set the byte codes are drawn. As is true of the compiler 320, the virtual-machine process 350 may be specified by code stored on a local disk or some other machine-readable medium from which it is read into FIG. 1's RAM 140 to configure the computer system to implement the garbage collector and otherwise act as a virtual machine. Again, though, that code's persistent storage may instead be provided by a server system remote from the processor that implements the virtual machine, in which case the code would be transmitted, e.g., electrically or optically to the virtual-machine-implementing processor.
In some implementations, much of the virtual machine's action in executing these byte codes is most like what those skilled in the art refer to as “interpreting,” so FIG. 3 depicts the virtual machine as including an “interpreter” 360 for that purpose. In addition to or instead of running an interpreter, many virtual-machine implementations actually compile the byte codes concurrently with the resultant object code's execution, so FIG. 3 depicts the virtual machine as additionally including a “just-in-time” compiler 370. The arrangement of FIG. 3 differs from FIG. 2 in that the compiler 320 for converting the human programmer's code does not contribute to providing the garbage collection function; that results largely from the virtual machine 350's operation.
Those skilled in that art will recognize that both of these organizations are merely exemplary, and many modern systems employ hybrid mechanisms, which partake of the characteristics of traditional compilers and traditional interpreters both. The invention to be described below is applicable independently of whether a batch compiler, a just-in-time compiler, an interpreter, or some hybrid is employed to process source code. In the remainder of this application, therefore, we will use the term compiler to refer to any such mechanism, even if it is what would more typically be called an interpreter.
Now, some of the functionality that source-language constructs specify can be quite complicated, requiring many machine-language instructions for their implementation. One quite-common example is a source-language instruction that calls for 64-bit arithmetic on a 32-bit machine. More germane to the present invention is the operation of dynamically allocating space to a new object; this may require determining whether enough free memory space is available to contain the new object and reclaiming space if there is not.
In such situations, the compiler may produce “inline” code to accomplish these operations. That is, all object-code instructions for carrying out a given source-code-prescribed operation will be repeated each time the source code calls for the operation. But inlining runs the risk that “code bloat” will result if the operation is invoked at many source-code locations.
The natural way of avoiding this result is instead to provide the operation's implementation as a procedure, i.e., a single code sequence that can be called from any location in the program. In the case of compilers, a collection of procedures for implementing many types of source-code-specified operations is called a runtime system for the language. The compiler and its runtime system are designed together so that the compiler “knows” what runtime-system procedures are available in the target computer system and can cause desired operations simply by including calls to procedures that the target system already contains. To represent this fact, FIG. 3 includes block 380 to show that the compiler's output makes calls to the runtime system as well as to the operating system 390, which consists of procedures that are similarly system resident but are not compiler-dependent.
Although the FIG. 3 arrangement is a popular one, it is by no means universal, and many further implementation types can be expected. Proposals have even been made to implement the virtual machine 350's behavior in a hardware processor, in which case the hardware itself would provide some or all of the garbage collection function. In short, garbage collectors can be implemented in a wide range of combinations of hardware and/or software.
By implementing garbage collection, a computer system can greatly reduce the occurrence of memory leaks and other software deficiencies in which human programming frequently results. But it can also have significant adverse performance effects if it is not implemented carefully. To distinguish the part of the program that does “useful” work from that which does the garbage collection, the term mutator is sometimes used in discussions of these effects; from the collector's point of view, what the mutator does is mutate active data structures' connectivity.
Some garbage collection approaches rely heavily on interleaving garbage collection steps among mutator steps. In one type of garbage collection approach, for instance, the mutator operation of writing a reference is followed immediately by garbage collector steps used to maintain a reference count in that object's header, and code for subsequent new-object storage includes steps for finding space occupied by objects whose reference count has fallen to zero. Obviously, such an approach can slow mutator operation significantly.
Other approaches therefore interleave very few garbage collector-related instructions into the main mutator process but instead interrupt it from time to time to perform garbage collection intervals, in which the garbage collector finds unreachable objects and reclaims their memory space for reuse. Such an approach will be assumed in discussing FIG. 4's depiction of a simple garbage collection operation. Within the memory space allocated to a given application is a part 420 managed by automatic garbage collection. As used hereafter, all dynamically allocated memory associated with a process or thread will be referred to as its heap. During the course of the application's execution, space is allocated for various objects 402, 404, 406, 408, and 410. Typically, the mutator allocates space within the heap by invoking the garbage collector, which at some level manages access to the heap. Basically, the mutator asks the garbage collector for a pointer to a heap region where it can safely place the object's data. The garbage collector keeps track of the fact that the thus-allocated region is occupied. It will refrain from allocating that region in response to any other request until it determines that the mutator no longer needs the region allocated to that object.
Garbage collectors vary as to which objects they consider reachable and unreachable. For the present discussion, though, an object will be considered “reachable” if it is referred to, as object 402 is, by a reference in a root set 400. The root set consists of reference values stored in the mutator's threads' call stacks, the central processing unit (CPU) registers, and global variables outside the garbage collected heap. An object is also reachable if it is referred to, as object 406 is, by another reachable object (in this case, object 402). Objects that are not reachable can no longer affect the program, so it is safe to re-allocate the memory spaces that they occupy.
A typical approach to garbage collection is therefore to identify all reachable objects and reclaim any previously allocated memory that the reachable objects do not occupy. A typical garbage collector may identify reachable objects by tracing references from the root set 400. For the sake of simplicity, FIG. 4 depicts only one reference from the root set 400 into the heap 420. (Those skilled in the art will recognize that there are many ways to identify references, or at least data contents that may be references.) The collector notes that the root set points to object 402, which is therefore reachable, and that reachable object 402 points to object 406, which therefore is also reachable. But those reachable objects point to no other objects, so objects 404, 408, and 410 are all unreachable, and their memory space may be reclaimed.
To avoid excessive heap fragmentation, some garbage collectors additionally relocate reachable objects. FIG. 5 shows a typical approach for this “copying” type of garbage collection. The heap is partitioned into two halves, hereafter called “semi-spaces.” For one garbage collection cycle, all objects are allocated in one semi-space 510, leaving the other semi-space 520 free. When the garbage collection cycle occurs, objects identified as reachable are “evacuated” to the other semi-space 520, so all of semi-space 510 is then considered free. Once the garbage collection cycle has occurred, all new objects are allocated in the lower semi-space 520 until yet another garbage collection cycle occurs, at which time the reachable objects are evacuated back to the upper semi-space 510.
Although this relocation requires the extra steps of copying the reachable objects and updating references to them, it tends to be quite efficient, since most new objects quickly become unreachable, so most of the current semi-space is actually garbage. That is, only a relatively few, reachable objects need to be relocated, after which the entire semi-space contains only garbage and can be pronounced free for reallocation.
Now, a collection cycle can involve following all reference chains from the basic root set—i.e., from inherently reachable locations such as the call stacks, class statics and other global variables, and registers—and reclaiming all space occupied by objects not encountered in the process. And the simplest way of performing such a cycle is to interrupt the mutator to provide a collector interval in which the entire cycle is performed before the mutator resumes. For certain types of applications, this approach to collection-cycle scheduling is acceptable and, in fact, highly efficient.
For many interactive and real-time applications, though, this approach is not acceptable. The delay in mutator operation that the collection cycle's execution causes can be annoying to a user and can prevent a real-time application from responding to its environment with the required speed. In some applications, choosing collection times opportunistically can reduce this effect. For example, a garbage-collection cycle may be performed at a natural stopping point in the application, such as when the mutator awaits user input.
So it may often be true that the garbage-collection operation's effect on performance can depend less on the total collection time than on when collections actually occur. But another factor that often is even more determinative is the duration of any single collection interval, i.e., how long the mutator must remain quiescent at any one time. In an interactive system, for instance, a user may never notice hundred-millisecond interruptions for garbage collection, whereas most users would find interruptions lasting for two seconds to be annoying.
The cycle may therefore be divided up among a plurality of collector intervals. When a collection cycle is divided up among a plurality of collection intervals, it is only after a number of intervals that the collector will have followed all reference chains and be able to identify as garbage any objects not thereby reached. This approach is more complex than completing the cycle in a single collection interval; the mutator will usually modify references between collection intervals, so the collector must repeatedly update its view of the reference graph in the midst of the collection cycle. To make such updates practical, the mutator must communicate with the collector to let it know what reference changes are made between intervals.
An even more complex approach, which some systems use to eliminate discrete pauses or maximize resource-use efficiency, is to execute the mutator and collector in concurrent execution threads. Most systems that use this approach use it for most but not all of the collection cycle; the mutator is usually interrupted for a short collector interval, in which a part of the collector cycle takes place without mutation.
Independent of whether the collection cycle is performed concurrently with mutator operation, is completed in a single interval, or extends over multiple intervals is the question of whether the cycle is complete, as has tacitly been assumed so far, or is instead “incremental.” In incremental collection, a collection cycle constitutes only an increment of collection: the collector does not follow all reference chains from the basic root set completely. Instead, it concentrates on only a portion, or collection set, of the heap. Specifically, it identifies every collection-set object referred to by a reference chain that extends into the collection set from outside of it, and it reclaims the collection-set space not occupied by such objects, possibly after evacuating them from the collection set.
By thus culling objects referenced by reference chains that do not necessarily originate in the basic root set, the collector can be thought of as expanding the root set to include as roots some locations that may not be reachable. Although incremental collection thereby leaves “floating garbage,” it can result in relatively low pause times even if entire collection increments are completed during respective single collection intervals.
Most collectors that employ incremental collection operate in “generations” although this is not necessary in principle. Different portions, or generations, of the heap are subject to different collection policies. New objects are allocated in a “young” generation, and older objects are “promoted” from younger generations to older or more “mature” generations. Collecting the younger generations more frequently than the others yields greater efficiency because the younger generations tend to accumulate garbage faster; newly allocated objects tend to “die,” while older objects tend to “survive.”
But generational collection greatly increases what is effectively the root set for a given generation. Consider FIG. 6, which depicts a heap as organized into three generations 620, 640, and 660. Assume that generation 640 is to be collected. The process for this individual generation may be more or less the same as that described in connection with FIGS. 4 and 5 for the entire heap, with one major exception. In the case of a single generation, the root set must be considered to include not only the call stack, registers, and global variables represented by set 600 but also objects in the other generations 620 and 660, which themselves may contain references to objects in generation 640. So pointers must be traced not only from the basic root set 600 but also from objects within the other generations.
One could perform this tracing by simply inspecting all references in all other generations at the beginning of every collection interval, and it turns out that this approach is actually feasible in some situations. But it takes too long in other situations, so workers in this field have employed a number of approaches to expediting reference tracing. One approach is to include so-called write barriers in the mutator process. A write barrier is code added to a write operation in the mutator code to record information from which the garbage collector can determine where references were written or may have been since the last collection interval. The write-barrier code may communicate this information directly to the collector or indirectly through other runtime processes. A list of modified references can then be maintained by taking such a list as it existed at the end of the previous collection interval and updating it by inspecting only locations identified by the write barriers as possibly modified since the last collection interval.
One of the many write-barrier implementations commonly used by workers in this art employs what has been referred to as the “card table.” FIG. 6 depicts the various generations as being divided into smaller sections, known for this purpose as “cards.” Card tables 610, 630, and 650 associated with respective generations contain an entry for each of their cards. When the mutator writes a reference in a card, it makes an appropriate entry in the card-table location associated with that card (or, say, with the card in which the object containing the reference begins). Most write-barrier implementations simply make a Boolean entry indicating that the write operation has been performed, although some may be more elaborate. For example, assume reference 624 on card 622 is modified (“dirtied”) by the mutator, so a Boolean entry in corresponding card-table entry 605 may be set accordingly. The mutator having thus left a record of where new or modified references may be, the collector may scan the card-table to identify those cards in the mature generation that were marked as having been modified since the last collection interval, and the collector can scan only those identified cards for modified references.
Of course, there are other write-barrier approaches, such as simply having the write barrier add to a list of addresses where references were written. Also, although there is no reason in principle to favor any particular number of generations, and although FIG. 6 shows three, most generational garbage collectors have only two generations, of which one is the young generation and the other is the mature generation. Moreover, although FIG. 6 shows the generations as being of the same size, a more-typical configuration is for the young generation to be considerably smaller. Further, each generation may be dispersed over various address ranges of memory instead of comprising a contiguous block of memory as shown in FIG. 6. Finally, although we assumed for the sake of simplicity that collection during a given interval was limited to only one generation, a more-typical approach is actually to collect the whole young generation at every interval but to collect the mature one less frequently.
Some collectors collect the entire young generation in every interval and may thereafter collect the mature generation collection in the same interval. It may therefore take relatively little time to scan all young-generation objects remaining after young-generation collection to find references into the mature generation. Even when such collectors do use card tables, therefore, they often do not use them for finding young-generation references that refer to mature-generation objects. On the other hand, laboriously scanning the entire mature generation for references to young-generation (or mature-generation) objects would ordinarily take too long, so write barriers are typically used to set card-table entries associated with the mature generation to thereby limit the amount of memory the collector searches for modified mature-generation references.
Write barrier code is often inserted into mutator code in close proximity to a corresponding mutator instruction that modifies a reference. In an imprecise card-marking scheme, the write barrier code marks the card-table entry that corresponds to the card in which the modified object begins. In a precise card-marking scheme, the write barrier marks the card-table entry that corresponds to the card in which the modified field is located. FIG. 7 illustrates exemplary write barrier code for precise card-marking that corresponds to a mutator instruction that modifies a reference.
FIG. 7's line N+1 contains an assembly instruction (STW) for storing a word-length value into an object reference field located at an offset C from the object's starting address, while lines N+3 through N+5 illustrate the assembly instruction's corresponding write-barrier code. In this example, the write barrier adds three instructions not originally present in the mutator code: ADD, Shift Right Logical (SRL) and Store Byte (STB) instructions. Specifically, the instruction at line N+3 stores the address of the modified object field in a “working” register, and the instruction at line N+4 divides this address by the card size to determine how many cards into the mature generation the modified field is located. Here, we have assumed the card size is 2M bytes. Lastly, the instruction at line N+5 marks a card-table entry with a binary “0” corresponding to the card in the mature generation that stores the modified object field. As described, each card-table entry is assumed to have a length of one byte.
As seen with regards to FIG. 7, the inclusion of write barriers after modifying object references increases the amount of mutator code, e.g., by three instructions per reference modification. Clearly, this overhead may significantly increase the mutator's execution time, especially when the mutator code modifies references frequently. So adding write barriers to increase the garbage collector's efficiency tends to compromise the mutator's.